﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Dos.ORM初级玩法demo之mvc简单三层</title>
    <link href="~/Content/pagination.css" rel="stylesheet" />
    <link href="~/Content/Bootstrap/bootstrap.min.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.11.1.min.js"></script>
    <script src="~/Scripts/Common.js"></script>
    <script src="~/Scripts/avalon.min.js"></script>
    <script src="~/Scripts/jquery.pagination.js"></script>
    <script src="~/Content/Bootstrap/bootstrap.min.js"></script>
    <script type="text/javascript">
        var avBody = avalon.define("avBody", function (vm) {
            vm.SearchName = "";
            vm.SearchIDNumber = "";
            vm.SearchMobilePhone = "";
            vm.Name = "";
            vm.IDNumber = "";
            vm.MobilePhone = "";
            vm.Users = [];
            vm.PageSize = 5;
            vm.PageIndex = 0;
            //获取数据
            vm.GetUser = function () {
                $.post("Home/GetUser", {
                    SearchName: avBody.SearchName,
                    SearchIDNumber: avBody.SearchIDNumber,
                    SearchMobilePhone: avBody.SearchMobilePhone,
                    _PageIndex: avBody.PageIndex + 1,
                    _PageSize: avBody.PageSize
                }, function (result) {
                    if (result.IsSuccess) {
                        avBody.Users = result.Data;
                        $("#divPager").pagination(result.DataCount, {
                            callback: avBody.PageSelectCallback,
                            items_per_page: avBody.PageSize,
                            current_page: avBody.PageIndex
                        });
                    } else {
                        alert("获取数据失败！异常信息：" + result.Message);
                    }
                });
            };
            //分页事件
            vm.PageSelectCallback = function (pageIndex) {
                avBody.PageIndex = pageIndex;
                $.post("Home/GetUser", {
                    SearchName: avBody.SearchName,
                    SearchIDNumber: avBody.SearchIDNumber,
                    SearchMobilePhone: avBody.SearchMobilePhone,
                    _PageIndex: avBody.PageIndex + 1,
                    _PageSize: avBody.PageSize
                }, function (result) {
                    if (result.IsSuccess) {
                        avBody.Users = result.Data;
                    } else {
                        alert("获取数据失败！异常信息：" + result.Message);
                    }
                });
            };
            //新增数据
            vm.AddUser = function () {
                $.post("Home/AddUser", {
                    Name: avBody.Name,
                    IDNumber: avBody.IDNumber,
                    MobilePhone: avBody.MobilePhone
                }, function (result) {
                    if (result.IsSuccess) {
                        avBody.GetUser();
                        alert("新增成功！");
                    } else {
                        alert("新增失败！异常信息：" + result.Message);
                    }
                });
            };
            //删除数据
            vm.DelUser = function (el) {
                if (confirm("确定删除【" + el.Name + "】？")) {
                    $.post("Home/DelUser", {
                        Id: el.Id
                    }, function (result) {
                        if (result.IsSuccess) {
                            avBody.GetUser();
                            alert("删除成功！");
                        } else {
                            alert("删除失败！异常信息：" + result.Message);
                        }
                    });
                }
            };
            //修改数据
            vm.UptUser = function (el) {
                $.post("Home/UptUser", {
                    Id: el.Id,
                    Name: $("#Name_" + el.Id).val(),
                    IDNumber: $("#IDNumber_" + el.Id).val(),
                    MobilePhone: $("#MobilePhone_" + el.Id).val()
                }, function (result) {
                    if (result.IsSuccess) {
                        avBody.GetUser();
                        alert("修改成功！");
                    } else {
                        alert("修改失败！异常信息：" + result.Message);
                    }
                });
            };
        });
        $(function () {
            avBody.GetUser();
        });
    </script>
</head>
<body ms-controller="avBody" style="padding: 10px;">
<h2>Dos.ORM初级玩法demo之mvc简单三层</h2>
<p>
    姓名：<input type="text" ms-duplex="SearchName"/>
    身份证号：<input type="text" ms-duplex="SearchIDNumber"/>
    手机号：<input type="text" ms-duplex="SearchMobilePhone"/>
    <input value="查询" type="button" class="btn btn-primary btn-xs" ms-click="GetUser"/>
</p>
<p>
    <table class="table table-bordered table-hover">
        <tr>
            <td>序号</td>
            <td>姓名</td>
            <td>身份证号</td>
            <td>手机号</td>
            <td>添加时间</td>
            <td>操作</td>
        </tr>
        <tr ms-repeat="Users">
            <td>{{$index + 1}}</td>
            <td>
                <input type="text" ms-value="el.Name" ms-attr-id="'Name_'+el.Id"/></td>
            <td>
                <input type="text" ms-value="el.IDNumber" ms-attr-id="'IDNumber_'+el.Id"/></td>
            <td>
                <input type="text" ms-value="el.MobilePhone" ms-attr-id="'MobilePhone_'+el.Id"/></td>
            <td>
                {{ShiftDate(el.CreateTime).Format('yyyy-MM-dd HH:mm:ss')}}
            <td>
                <input value="修改" type="button" class="btn btn-primary btn-xs" ms-click="UptUser(el)"/>
                <input value="删除" type="button" class="btn btn-primary btn-xs" ms-click="DelUser(el)"/>
            </td>
        </tr>
    </table>
</p>
<p id="divPager"></p>
<p>
    姓名：<input type="text" ms-duplex="Name"/>
    身份证号：<input type="text" ms-duplex="IDNumber"/>
    手机号：<input type="text" ms-duplex="MobilePhone"/>
    <input value="新增" type="button" class="btn btn-primary btn-xs" ms-click="AddUser"/>
</p>
<p>
    <a href="http://www.itdos.com/CSharp/20150803/0219373.html" target="_blank" style="font-size: 18px; font-weight: bold; ">http://www.itdos.com/CSharp/20150803/0219373.html</a>
</p>
</body>
</html>
